<?php
/**
 * User: aka
 * Date: 2018/6/24
 * Time: 14:55
 */

namespace app\common\request;

use think\Loader;

class RequestOperation
{
    public $request;

    public $isAjaxRequest;

    public $isMobileRequest;

    public $getAllRequestParam;

    public $getUserIp;

    public function __construct()
    {
        $this->request = \think\Request::instance();
    }

    public function __destruct()
    {
        unset($this->request);
        unset($this->isAjaxRequest);
        unset($this->isMobileRequest);
        unset($this->getAllRequestParam);
        unset($this->getUserIp);
    }

    public function getAllRequestParam()
    {
        return $this->request->param();
    }

    public function getUserIp()
    {
        return $this->request->ip();
    }

    public function isAjaxRequest()
    {
        return $this->request->isAjax();
    }

    public function isMobileRequest()
    {
        return $this->request->isMobile();
    }

    public function getToken()
    {
        echo json_encode($this->request->token());
    }

    public function requestValidate(string $validate,array $requestData)
    {
        $validate = Loader::validate($validate);
        $result   = $validate->check($requestData);
        return $result ? true : $this->errorMsgToJson($validate->getError());
    }

    public function errorMsgToJson($data)
    {
        echo json_encode($data);
        die();
    }

    public function successCodeToJson($code)
    {
        echo json_encode($code);
    }

    public function errorCodeToJson($code)
    {
        echo json_encode($code);
    }
}